#include "PartManager.h"
#include "ui_PartManager.h"

PartManager::PartManager(QMainWindow *parent)
    : QMainWindow(parent)
    , ui(new Ui::PartManager)
{
    PartManager::initMenu();
}

PartManager::PartManager(const User &user, QMainWindow *parent)
    : QMainWindow(parent)
    , ui(new Ui::PartManager),m_operatorUserInfo(user)
{
    PartManager::initMenu();
}

PartManager::~PartManager()
{
    delete ui;
}

void PartManager::initMenu()
{
    ui->setupUi(this);

    m_DBManager = DatabaseManager::getInstance();
    try{
        m_DBManager->initDatabase();
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,"数据库连接初始化提示",e.what());
    }

    ui->lb_operatorName->setText(m_operatorUserInfo.getName());
    ui->lb_operatorRole->setText(reRoleMap[m_operatorUserInfo.getRole()]);

    connect(ui->tbtn_exit,&QToolButton::clicked,this,&PartManager::close);

    connect(ui->tbtn_partInfoManage,&QToolButton::clicked,this,[this]()
            {
                ui->stackedWidget->setCurrentWidget(ui->page_infoManage);
            });

    connect(ui->tbtn_partAllotManage,&QToolButton::clicked,this,[this]()
            {
                if(!m_operatorUserInfo.check_permission(UPBits::USER_INFO_ASSIGNING))
                {
                    QMessageBox::information(this,"权限相关提示","你没有分配配件的权限");
                    return;
                }
                ui->stackedWidget->setCurrentWidget(ui->page_allotManage);
            });

    ui->stackedWidget->setCurrentWidget(ui->page_infoManage);

    PartManager::initInfoManagePageMenu();
    PartManager::initAlloManagePageMenu();
}

void PartManager::initInfoManagePageMenu()
{
    connect(ui->infoManage_cb_InventoryLimit,&QCheckBox::clicked,this,[this](bool state)
            {
                ui->infoManage_sb_lowerLimit->setEnabled(state);
                ui->infoManage_sb_upperLimit->setEnabled(state);
            });

    m_infoSqlQueryModel = new QSqlQueryModel(this);
    ui->infoManage_tv_partInfoList->setModel(m_infoSqlQueryModel);

    PartManager::m_on_infoManage_btn_refresh_clicked();

    m_infoSqlQueryModel->setHeaderData(0,Qt::Horizontal,tr("ID"));
    m_infoSqlQueryModel->setHeaderData(1,Qt::Horizontal,tr("配件名称"));
    m_infoSqlQueryModel->setHeaderData(2,Qt::Horizontal,tr("配件型号"));
    m_infoSqlQueryModel->setHeaderData(3,Qt::Horizontal,tr("价格"));
    m_infoSqlQueryModel->setHeaderData(4,Qt::Horizontal,tr("位置"));
    m_infoSqlQueryModel->setHeaderData(5,Qt::Horizontal,tr("库存数量"));
    m_infoSqlQueryModel->setHeaderData(6,Qt::Horizontal,tr("库存上限"));
    m_infoSqlQueryModel->setHeaderData(7,Qt::Horizontal,tr("库存下限"));

    ui->infoManage_tv_partInfoList->setSelectionBehavior(QAbstractItemView::SelectRows); // 单击选中整行
    ui->infoManage_tv_partInfoList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//宽度自动伸展
    ui->infoManage_tv_partInfoList->verticalHeader()->setVisible(false);//不显示行号

    connect(ui->infoManage_tv_partInfoList,&QTableView::clicked,this,&PartManager::m_on_infoManage_tv_partInfoList_clicked);
    connect(ui->infoManage_btn_add,&QPushButton::clicked,this,&PartManager::m_on_infoManage_btn_add_clicked);
    connect(ui->infoManage_btn_delete,&QPushButton::clicked,this,&PartManager::m_on_infoManage_btn_delete_clicked);
    connect(ui->infoManage_btn_update,&QPushButton::clicked,this,&PartManager::m_on_infoManage_btn_update_clicked);
    connect(ui->infoManage_btn_find,&QPushButton::clicked,this,&PartManager::m_on_infoManage_btn_find_clicked);
    connect(ui->infoManage_btn_refresh,&QPushButton::clicked,this,&PartManager::m_on_infoManage_btn_refresh_clicked);
}

void PartManager::initAlloManagePageMenu()
{
    m_allotSqlQueryModel = new QSqlQueryModel(this);
    ui->allotManage_tv_noAllotPartInfoList->setModel(m_allotSqlQueryModel);

    PartManager::m_on_allotManage_tbt_refresh();

    m_allotSqlQueryModel->setHeaderData(0,Qt::Horizontal,tr("ID"));
    m_allotSqlQueryModel->setHeaderData(1,Qt::Horizontal,tr("配件名称"));
    m_allotSqlQueryModel->setHeaderData(2,Qt::Horizontal,tr("配件型号"));
    m_allotSqlQueryModel->setHeaderData(3,Qt::Horizontal,tr("价格"));
    m_allotSqlQueryModel->setHeaderData(4,Qt::Horizontal,tr("位置"));
    m_allotSqlQueryModel->setHeaderData(5,Qt::Horizontal,tr("库存数量"));
    m_allotSqlQueryModel->setHeaderData(6,Qt::Horizontal,tr("库存上限"));
    m_allotSqlQueryModel->setHeaderData(7,Qt::Horizontal,tr("库存下限"));

    ui->allotManage_tv_noAllotPartInfoList->setSelectionBehavior(QAbstractItemView::SelectRows); // 单击选中整行
    ui->allotManage_tv_noAllotPartInfoList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//宽度自动伸展
    ui->allotManage_tv_noAllotPartInfoList->verticalHeader()->setVisible(false);//不显示行号

    connect(ui->allotManage_tv_noAllotPartInfoList,&QTableView::clicked,this,&PartManager::m_on_allotManage_tv_noAllotPartInfoList_clicked);
    connect(ui->allotManage_btn_allot,&QPushButton::clicked,this,&PartManager::m_on_allotManage_btn_allot_clicked);
    connect(ui->allotManage_tbt_refresh,&QPushButton::clicked,this,&PartManager::m_on_allotManage_tbt_refresh);

}

void PartManager::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_selectPartInfoFlag = false;

        ui->infoManage_le_id->clear();
        ui->infoManage_le_name->clear();
        ui->infoManage_le_model->clear();
        ui->infoManage_dsb_price->clear();
        ui->infoManage_le_location->clear();
        ui->infoManage_sb_quantity->clear();
        ui->infoManage_sb_upperLimit->clear();
        ui->infoManage_sb_lowerLimit->clear();

        ui->allotManage_le_id->clear();
        ui->allotManage_le_newLocation->clear();
    }
    QWidget::mouseReleaseEvent(event);

    qDebug() << "鼠标松开事件，是否选中数据:" << m_selectPartInfoFlag;
}

void PartManager::m_on_infoManage_tv_partInfoList_clicked(const QModelIndex &index)
{
    ui->infoManage_le_id->setText(m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),0)).toString());
    ui->infoManage_le_name->setText(m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),1)).toString());
    ui->infoManage_le_model->setText(m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),2)).toString());
    ui->infoManage_dsb_price->setValue(m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),3)).toDouble());
    ui->infoManage_le_location->setText(m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),4)).toString());
    ui->infoManage_sb_quantity->setValue(m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),5)).toInt());
    ui->infoManage_sb_upperLimit->setValue(m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),6)).toInt());
    ui->infoManage_sb_lowerLimit->setValue(m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),7)).toInt());

    m_selectPartInfoFlag = true;
    m_selectPartInfoId = m_infoSqlQueryModel->data(this->m_infoSqlQueryModel->index(index.row(),0)).toLongLong();

    qDebug() << "点击列表，是否选中数据:" << m_selectPartInfoFlag << m_selectPartInfoId;
}

void PartManager::m_on_infoManage_btn_add_clicked()
{
    QString hintTitle = "添加配件信息提示";
    m_selectPartInfoFlag = false;

    if(!m_operatorUserInfo.check_permission(UPBits::USER_INFO_ADD))
    {
        QMessageBox::information(this,"权限相关提示","你没有权限添加配件记录");
        return;
    }

    try{
        m_DBManager->partManage_insertPartInfoRecord(Part(ui->infoManage_le_name->text(),
                                                          ui->infoManage_le_model->text(),
                                                          ui->infoManage_dsb_price->value(),
                                                          ui->infoManage_le_location->text(),
                                                          ui->infoManage_sb_quantity->value(),
                                                          ui->infoManage_sb_upperLimit->value(),
                                                          ui->infoManage_sb_lowerLimit->value()));
        m_DBManager->logManage_insertLogRecord(LogEntry(QDateTime::currentDateTime(),
                                                        m_operatorUserInfo.getName(),
                                                        m_operatorUserInfo.getRole(),
                                                        QString("添加了一条配件信息:[add]%1").arg(ui->infoManage_le_name->text())));
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,hintTitle,e.what());
        return;
    }

    PartManager::m_on_infoManage_btn_refresh_clicked();

    QMessageBox::information(this,hintTitle,QString("添加%1配件成功").arg(ui->infoManage_le_name->text()));
}

void PartManager::m_on_infoManage_btn_delete_clicked()
{
    QString hintTitle = "删除配件信息提示";
    m_selectPartInfoFlag = false;

    if(!m_operatorUserInfo.check_permission(UPBits::USER_INFO_DELETE))
    {
        QMessageBox::information(this,"权限相关提示","你没有权限删除配件记录");
        return;
    }

    try{
        m_DBManager->partManage_deletePartInfoRecord(m_selectPartInfoId);
        m_DBManager->logManage_insertLogRecord(LogEntry(QDateTime::currentDateTime(),
                                                        m_operatorUserInfo.getName(),
                                                        m_operatorUserInfo.getRole(),
                                                        QString("删除了一条配件信息:[delete]%1").arg(ui->infoManage_le_name->text())));
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,hintTitle,e.what());
        return;
    }

    PartManager::m_on_infoManage_btn_refresh_clicked();

    QMessageBox::information(this,hintTitle,QString("删除%1配件成功").arg(ui->infoManage_le_name->text()));
}

void PartManager::m_on_infoManage_btn_update_clicked()
{
    QString hintTitle = "修改配件信息提示";
    m_selectPartInfoFlag = false;

    if(!m_operatorUserInfo.check_permission(UPBits::USER_INFO_MODIFY))
    {
        QMessageBox::information(this,"权限相关提示","你没有修改配件信息记录的权限");
        return;
    }

    try{
        m_DBManager->partManage_updatePartInfoRecord(Part(ui->infoManage_le_name->text(),
                                                          ui->infoManage_le_model->text(),
                                                          ui->infoManage_dsb_price->value(),
                                                          ui->infoManage_le_location->text(),
                                                          ui->infoManage_sb_quantity->value(),
                                                          ui->infoManage_sb_upperLimit->value(),
                                                          ui->infoManage_sb_lowerLimit->value()),m_selectPartInfoId);
        m_DBManager->logManage_insertLogRecord(LogEntry(QDateTime::currentDateTime(),
                                                        m_operatorUserInfo.getName(),
                                                        m_operatorUserInfo.getRole(),
                                                        QString("修改了一条配件信息:[update]%1").arg(ui->infoManage_le_name->text())));
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,hintTitle,e.what());
        return;
    }

    PartManager::m_on_infoManage_btn_refresh_clicked();

    QMessageBox::information(this,hintTitle,QString("修改%1配件成功").arg(ui->infoManage_le_name->text()));
}

void PartManager::m_on_infoManage_btn_find_clicked()
{
    QString hintTitle = "查找配件信息提示";
    m_selectPartInfoFlag = false;

    if(!m_operatorUserInfo.check_permission(UPBits::USER_INFO_FIND))
    {
        QMessageBox::information(this,"权限相关提示","你没有查找配件信息的权限");
        return;
    }

    try{
        m_infoSqlQueryModel->setQuery(m_DBManager->partManage_findPartInfoRecord(ui->infoManage_le_find->text()));
        m_DBManager->logManage_insertLogRecord(LogEntry(QDateTime::currentDateTime(),
                                                        m_operatorUserInfo.getName(),
                                                        m_operatorUserInfo.getRole(),
                                                        QString("查询了配件信息:[find]%1").arg(ui->infoManage_le_find->text())));
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,hintTitle,e.what());
        return;
    }
}

void PartManager::m_on_infoManage_btn_refresh_clicked()
{
    if(!m_operatorUserInfo.check_permission(UPBits::USER_INFO_SHOW))
    {
        QMessageBox::information(this,"权限相关提示","你没有权限查看配件记录");
        return;
    }

    try{
        m_infoSqlQueryModel->setQuery(m_DBManager->getQueryForAll("part"));
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,"刷新内容提示",e.what());
    }
}

void PartManager::m_on_allotManage_tv_noAllotPartInfoList_clicked(const QModelIndex &index)
{
    ui->allotManage_le_id->setText(m_allotSqlQueryModel->data(m_allotSqlQueryModel->index(index.row(),0)).toString());

    m_selectAllotPartInfoFlag = true;
    m_selectAllotPartInfoId = m_allotSqlQueryModel->data(m_allotSqlQueryModel->index(index.row(),0)).toLongLong();
}

void PartManager::m_on_allotManage_btn_allot_clicked()
{
    QString hintTitle = "分配配件位置提示";

    if(!m_operatorUserInfo.check_permission(UPBits::USER_INFO_ASSIGNING))
    {
        QMessageBox::information(this,"权限相关提示","你没有分配配件的权限");
    }

    if(ui->allotManage_le_newLocation->text().isEmpty())
    {
        QMessageBox::information(this,hintTitle,"分配位置不能为空");
        return;
    }
    if(ui->allotManage_le_id->text().isEmpty())
    {
        QMessageBox::information(this,hintTitle,"请先选择一个要分配的配件记录");
        return;
    }

    m_selectAllotPartInfoFlag = false;

    try{
        m_DBManager->allotManage_updateLocationInfoRecord(ui->allotManage_le_newLocation->text(),m_selectAllotPartInfoId);
        m_DBManager->logManage_insertLogRecord(LogEntry(QDateTime::currentDateTime(),
                                                        m_operatorUserInfo.getName(),
                                                        m_operatorUserInfo.getRole(),
                                                        QString("分配了配件信息:[allot]新位置为%1").arg(ui->allotManage_le_newLocation->text())));
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,hintTitle,e.what());
        return;
    }

    QMessageBox::information(this,hintTitle,QString("分配配件的新位置为%1成功").arg(ui->allotManage_le_newLocation->text()));
    PartManager::m_on_allotManage_tbt_refresh();
    PartManager::m_on_infoManage_btn_refresh_clicked();
}

void PartManager::m_on_allotManage_tbt_refresh()
{
    m_selectAllotPartInfoFlag = false;
    try{
        m_allotSqlQueryModel->setQuery(m_DBManager->allotManage_findPartInfoRecord("未分配"));
    }catch(const std::exception& e)
    {
        QMessageBox::critical(this,"刷新未分配配件信息记录提示",e.what());
    }
}
